var COS = require('cos-js-sdk-v5');

export default {
  data() {
    return {
      COSIns: null,
      targetBucket:{
        scenario:{
          bucket:"scenario-article-1307150208",
          path:"cover/"
        },
        "emoticon-material":{
          bucket:"emoticon-material-1307150208",
          path:"material/"
        },
        "emoticon-ability":{
          bucket:"emoticon-material-1307150208",
          path:"ability/"
        },
        "banner":{
          bucket:"emoticon-material-1307150208",
          path:"banner/"
        },
        "avatar-material":{
          bucket:"avatar-material-1307150208",
          path:"material/"
        },
        
        "avatar-ornament":{
          bucket:"avatar-material-1307150208",
          path:"ornament/"
        },
        "cases-emoticon":{
          bucket:"cases-1307150208",
          path:"emoticon/"
        },
        "cases-scenario":{
          bucket:"cases-1307150208",
          path:"scenario/"
        },
        "cases-avatar":{
          bucket:"cases-1307150208",
          path:"avatar/"
        },
        "general":{
          bucket:"currency-1307150208",
          path:""
        }
      },
			targetPath:{
				material:"material/",
				ability:"ability/"
			},
    };
  },
  mounted() {
    this.initCOS();
  },
  methods: {
    initCOS() {
      this.COSIns = new COS({
        SecretId: "AKIDEH4btvgBQO5Vr2xc3FBrHSN2SdYDcmgK",
        SecretKey: "PfeMmvvhDVYLs9C0YqseAQQK2VjiRu74",
      });
      console.log("tencent cos init!", this.COSIns);
    },
    cosUpload(fileObject,target,path = '') {
      let paths = fileObject.name.split(".");
      let suffix = "." + paths[paths.length - 1];
			let filename=this.guid()+suffix
      // 先选择文件，得到临时路径
      return new Promise((resolve, reject) => {
        this.COSIns.putObject(
          {
            Bucket: this.targetBucket[target].bucket,
            Region: "ap-shanghai",
						// StorageClass: 'STANDARD',
            Key: (path|| this.targetBucket[target].path) + filename,
						Body: fileObject, 
            onProgress: function (info) {
              console.log(JSON.stringify(info));
            },
          },
          function (err, data) {
            if (data.statusCode === 200) {
              resolve("https://"+data.Location);
            } else {
              reject(err.error);
            }
          }
        );
      });
    },
		guid() {
			function S4() {
					return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
			}
			return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
		}
  },
};
